package selenium;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class tengxun {
	// 乐视采集系列
	public static void main(String[] args) {
		File fileExist = new File("D:\\腾讯视频采集");
		if (!fileExist.exists()) {// 判断目录是否存在
			fileExist.mkdirs();
		}
		final WebDriver driver = new ChromeDriver();
		try {
			//driver.manage().window().maximize();
			
			//唯一需要更改的地方---------start
			driver.get("http://v.qq.com/x/list/ent");// 链接采集
			//唯一需要更改的地方---------end
			
			driver.findElement(By.linkText("最新上架")).click();
			WebElement aaa = driver.findElements(By.className("figures_list")).get(0);
			List<WebElement> bbb = aaa.findElements(By.tagName("li"));
			// filter_item current open
			Thread.sleep(1000);
			WebElement leimu = driver.findElement(By.cssSelector("[class='filter_item current open']")); // 当前类目元素
			String leimuString = leimu.getText();// 类目名字
			int a = 0;// 初始化数目

			Date now = new Date();// 获取系统当前时间
			Date yesterday = new Date(new Date().getTime()-24*60*60*1000);//昨天的日期
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");// 到日期为止
			String strDate = s.format(now);// 格式化日期转换为字符串
			String yesterdayDate = s.format(yesterday);// 格式化日期转换为字符串
			// item current
			WebElement leixing = driver.findElements(By.cssSelector("[class='item current']")).get(0); // 当前类目元素
			String leixingText = leixing.getText();// 类型分类

			// html/body/div[3]/div/div/div[1]/div[1]/div/div/span[1]/em

			WebElement v_num = driver
					.findElement(By.xpath("/html/body/div[3]/div/div/div[1]/div[1]/div/div/span[1]/em"));
			String v_numElement = v_num.getText();// 总数

			for (WebElement aaElement : bbb) {
				WebElement aeElement = aaElement.findElement(By.tagName("a"));
				String url = aeElement.getAttribute("href");
				String urlString = url.split("\\?")[0];
				String title = aaElement.findElements(By.tagName("a")).get(1).getAttribute("title").toString();
				String line = urlString + "●" + title;
				a += 1;
				System.out.println(a + "_排序_" + v_numElement + "_" + line);
				String txtExistString = "D:\\腾讯视频采集\\" + strDate + "【" + leimuString + "】" + "_" + leixingText + "_"
						+ v_numElement + ".txt";
				String txtExistStringYesterday = "D:\\腾讯视频采集\\" + yesterdayDate + "【" + leimuString + "】" + "_" + leixingText + "_"
						+ v_numElement + ".txt";
				File txtExist = new File(txtExistString);
				File txtExistYesterday = new File(txtExistStringYesterday);
				if ((txtExist.exists()&&a==1)||(txtExistYesterday.exists()&&a==1)) {// 判断目录是否存在
					System.out.println("今天或者昨天本类目已经开采过了，请不要重复采集");
					throw new RuntimeException();
				} else {
					method2("D:\\腾讯视频采集\\" + strDate + "【" + leimuString + "】" + "_" + leixingText + "_" + v_numElement
							+ ".txt", line);
				}
			}
			;
			for (int i = 2; i < 99999; i++) {
				// page_next disabled
				WebElement finalxx = driver.findElement(By.linkText("下一页"));// 已经到了最后一页
				String classEquals = finalxx.getAttribute("class");
				if (!"page_next disabled".equals(classEquals)) {
					driver.findElement(By.linkText("下一页")).click();
					WebElement aaaFor = driver.findElements(By.className("figures_list")).get(0);
					List<WebElement> bbbFor = aaaFor.findElements(By.tagName("li"));
					for (WebElement aaElement : bbbFor) {
						WebElement aeElement = aaElement.findElement(By.tagName("a"));
						String url = aeElement.getAttribute("href");
						String title = aaElement.findElements(By.tagName("a")).get(1).getAttribute("title").toString();
						String urlString = url.split("\\?")[0];
						String line = urlString + "●" + title;
						a += 1;
						System.out.println(a + "_排序_" + v_numElement +"_"+ line);
						method2("D:\\腾讯视频采集\\" + strDate + "【" + leimuString + "】" + "_" + leixingText + "_"
								+ v_numElement + ".txt", line);
					}
					;
				} else {
					break;
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		driver.quit();
	}

	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
